<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
            //<![CDATA[

var tgd = tgd || {};
tgd.Tools = tgd.Tools || {};
tgd.Tools.Auto = tgd.Tools.Auto || {};

tgd.Tools.Auto.Vehicle = function (callback) {
    var _engine = new tgd.Tools.Auto.Engine();
    var _trany = new tgd.Tools.Auto.Transmission();
    var _rearend = new tgd.Tools.Auto.Rearend();
    var _tire = new tgd.Tools.Auto.Tire();
    var _onChange = callback;

    this.__defineSetter__("RPM", function(val) { _engine.RPM = val;_onChange(); });
    this.__defineGetter__("RPM", function() { return _engine.RPM; });

    this.__defineSetter__("Transmission", function(val) { _trany.Gears = val;_onChange(); });
    this.__defineGetter__("Transmission", function() { return _trany.Gears; });

    this.__defineSetter__("Rearend", function(val) { _rearend.Ratio = val;_onChange(); });
    this.__defineGetter__("Rearend", function() { return _rearend.Ratio; });

    this.__defineSetter__("Tire", function(val) { _tire.Size = val;_onChange(); });
    this.__defineGetter__("Tire", function() { return _tire.Size; });

    this.__defineGetter__("Speed", function() {
        //ER = Engine Revolutions
        //DSR = Drive Shaft Revolutions
        //AR = Axel Revolutions

        return (_engine.RPM /* ER/min */ *
                60 /* min/hr */ *
                _trany.Gears /* DSR/ER */ *
                _rearend.Ratio /* AR/DSR */ *
                _tire.Size /* in/AR */ *
                (1/63360) /* mi/in */).toFixed(0);
    });
}

tgd.Tools.Auto.Engine = function() {
    var _rpm;

    this.__defineGetter__("RPM", function() { return _rpm; });
    this.__defineSetter__("RPM", function(val) { _rpm = val; });
}

tgd.Tools.Auto.Transmission = function() {
    var _gears = [];

    this.__defineGetter__("Gears", function() { return _gears; });
    this.__defineSetter__("Gears", function(val) { _gears = val; });
}

tgd.Tools.Auto.Rearend = function() {
    var _ratio;

    this.__defineGetter__("Ratio", function() { return _ratio; });
    this.__defineSetter__("Ratio", function(val) { _ratio = val; });
}

tgd.Tools.Auto.Tire = function() {
    var _size;  //circumference for now, make 245/60/40 option

    this.__defineGetter__("Size", function() { return _size; });
    this.__defineSetter__("Size", function(val) { _size = val; });
}

var car;

function init() {
    car = new tgd.Tools.Auto.Vehicle(
        function() {
            document.getElementById("txtSpeed").value = car.Speed;
            document.getElementById("txtRPM").value = car.RPM;
            document.getElementById("txtTransmission").value = car.Transmission;
            document.getElementById("txtRearend").value = car.Rearend;
            document.getElementById("txtTire").value = car.Tire;
        }
    );
    car.Rearend = 1/3.07;
    car.Transmission = [1.0];
    car.Tire = ((245 * .6 * 2 * .0393700787 + 14) * Math.PI);
    car.RPM = 2000;
    
    clearInterval(inty);

    var up = true;

    var foo = function() {
	    if (car.RPM > 7000 || car.RPM < 700) {
		    up = !up;
	    }

	    if (up) {
		    car.RPM += 100;
	    } else {
		    car.RPM -= 100;
	    }
    }


    var inty = setInterval(foo, 128);
    
}

    //]]>
        </script>
        <style type="text/css">
            body{margin:0;}
            input{border:none;font-size:3em;text-align:center;vertical-align:middle;}
        </style>
    </head>
    <body onload="init();">
        <div>
            <label for="txtSpeed">Speed</label>
            <input id="txtSpeed" type="text" />
        </div>
        <div>
            <label for="txtRPM">RPM</label>
            <input id="txtRPM" type="text" />
        </div>
        <div>
            <label for="txtTransmission">Transmission</label>
            <input id="txtTransmission" type="text" />
        </div>
        <div>
            <label for="txtRearend">Rearend</label>
            <input id="txtRearend" type="text" />
        </div>
        <div>
            <label for="txtTire">Tire</label>
            <input id="txtTire" type="text" />
        </div>
    </body>
</html>
